package com.djzhou.gmms.service.user.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.djzhou.gmms.model.entity.PageBean;
import com.djzhou.gmms.model.user.TbUser;
import com.djzhou.gmms.model.user.VoResource;
import com.djzhou.gmms.model.user.VoUser;
import com.djzhou.gmms.service.base.impl.BaseServiceImpl;
import com.djzhou.gmms.service.user.UserService;

public class UserServiceImpl extends BaseServiceImpl implements UserService{

	@Override
	public void add(TbUser t) {
		userDAO.insert(t);
		
	}

	@Override
	public void delete(Integer id) {
		TbUser user=userDAO.queryByID(TbUser.class, id);
		user.setDeleteFlag(1);
		userDAO.update(user);
		
	}

	@Override
	public TbUser queryById(Integer id) {
		return userDAO.queryByID(TbUser.class, id);
	}

	@Override
	public List<TbUser> queryList(HashMap<String, Object> hashMap) {
		String hql="from TbUser user where user.deleteFlag=0 and user.userId<>1";
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		if(hashMap!=null && hashMap.size()!=0){
			if(hashMap.get("userName")!=null && !hashMap.get("userName").equals(1)){
				sb.append(" and user.realName like '%").append(hashMap.get("userName").toString()).append("%'");
			}
			if(hashMap.get("departmentId")!=null && !hashMap.get("departmentId").equals(1)){
				sb.append(" and user.department.departmentId=").append(hashMap.get("departmentId").toString());
			}
		}
		sb.append(" order by user.realName asc");
		hql=sb.toString();
		return userDAO.query(hql);
	}

	@Override
	public PageBean<TbUser> queryPageBean(HashMap<String, Object> hashMap,
			int pageSize, int currentPage) {
		String hql="from TbUser user where user.deleteFlag=0 and user.userId<>1";
		String companyId=null;
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		if(hashMap!=null && hashMap.size()!=0){
			if(hashMap.get("companyId")!=null && !hashMap.get("companyId").equals(1)){
				companyId=hashMap.get("companyId").toString();
				sb.append(" and user.company.companyId=").append(companyId);
			}
			if(hashMap.get("departmentId")!=null && !hashMap.get("departmentId").equals(1)){
				sb.append(" and user.department.departmentId=").append(hashMap.get("departmentId").toString());
			}
			if(hashMap.get("userName")!=null && !hashMap.get("userName").equals(1)){
				sb.append(" and user.realName like '%").append(hashMap.get("userName").toString()).append("%'");
			}
		}
		sb.append(" order by createTime desc");
		hql=sb.toString();
		return userDAO.selectByPage(hql, pageSize, currentPage);
	}

	@Override
	public void update(TbUser t) {
		userDAO.update(t);
		
	}

	@Override
	public TbUser queryUser4Login(String username, String password) {
		String hql="from TbUser user where user.loginPwd=? and (user.loginName=? or user.userNo=?) and user.deleteFlag=0";
		List<TbUser> list=userDAO.query(hql, password,username,username);
		if(list!=null && list.size()!=0)return list.get(0);
		else return null;
	}

	@Override
	public synchronized String queryUserNo() {
		String sql="select MAX(USER_NO) AS USER_NO from TB_USER";
		Integer maxUserNo=null;
		try {
			maxUserNo=Integer.parseInt(userDAO.searchSql(sql, "USER_NO").toString());
			if(maxUserNo!=null)maxUserNo++;
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return maxUserNo.toString();
	}

	@Override
	public List<TbUser> queryList(Integer companyId,Integer userId) {
		if(companyId!=1){
			String hql="from TbUser user where user.deleteFlag=0 and user.company.companyId=? and user.userId<>?";
			return userDAO.query(hql, companyId,userId);
		}else{
			String hql="from TbUser user where user.deleteFlag=0 and user.userId<>?";
			return userDAO.query(hql, userId);
		}
		
	}

	@Override
	public VoResource query4AutoComplete(HashMap<String, Object> hashMap,String sidx,String sord,int pageSize, int currentPage) {
		String hql="from TbUser user where user.deleteFlag=0 and user.userId>1";
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		if(hashMap!=null && hashMap.size()!=0){
			if(hashMap.get("areaId")!=null){
				sb.append(" and user.areaId =").append(hashMap.get("areaId").toString());
			}
			if(hashMap.get("userName")!=null){
				sb.append(" and user.realName like '%").append(hashMap.get("userName").toString()).append("%'");
			}
			if(hashMap.get("roleIds")!=null ){
				sb.append(" and user.userId in (select ur.userId from TbUserRole ur where ur.roleId in(").append(hashMap.get("roleIds").toString()).append("))");
			}
		}
		hql=sb.toString();
		PageBean<TbUser> pageBean= userDAO.selectByPage(hql, pageSize, currentPage);
		VoResource resource=new VoResource();
		List<VoUser> voList=new ArrayList<VoUser>();
		VoUser vo=null;
		for(TbUser user:pageBean.getList()){
			vo=new VoUser();
			vo.setId(user.getUserId());
			vo.setName(user.getRealName());
			vo.setDepartmentName(user.getDepartment().getDepartmentName());
			vo.setPhone(user.getUserPhone());
			voList.add(vo);
		}
		resource.setPage(currentPage);
		resource.setRecords(pageBean.getAllRow());
		resource.setRows(voList);
		resource.setTotal(pageBean.getTotalPage());
		return resource;
	}

	@Override
	public boolean verifyUserIsExists(String loginName, Integer userId) {
		String hql="from TbUser user where user.loginName=? and user.userId<>? and user.deleteFlag=0";
		List<TbUser> list=userDAO.query(hql, loginName,userId);
		return list!=null&&list.size()!=0?false:true;
	}

	
}
